// pages/mall/receAddressEdit/receAddressEdit.js
import * as Api_Mall_Address from '../../../api/mall/address'
const config = require('../../../utils/config.js')
const QQMapWX = require('../../../utils/qqmap-wx-jssdk.min')
var qqMapSdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityList:['北京市','上海市','天津市','重庆市'],
    addr:{
      defaultFlag:'N',
      recipientName:'',
      phone:'',
      provinceName:'',
      cityName:'',
      areaName:'',
      streetName:'',
      addressName:'',
      provinceCode:'',
      cityCode:'',
      adcode:'',
      latidude:'',
      longitude:'',
    },
    adName:'',
    subState:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.loadDetail(options.id)
    }
  },
  loadDetail(e){
    Api_Mall_Address.getDetail(e).then(res=>{
      this.setData({
        addr:res.data,
        adName:res.data.provinceName+','+res.data.cityName+','+res.data.areaName
      })
    })
  },
  maxValue(val,length){
    console.log(val.length)
    return val.length >= length
  },
  changeName(e){
    let str = this.data.addr.recipientName
    // if(this.maxValue(str,2))
    //   return
    this.setData({
      'addr.recipientName':e.detail.value
    })
  },
  changePhone(e){
    this.setData({
      'addr.phone':e.detail.value
    })
  },
  changeAddr(e){
    this.setData({
      'addr.addressName':e.detail.value
    })
  },
  changeDefult(){
    let flag = ''
    if(this.data.addr.defaultFlag == 'N'){
      flag='Y'
    }else{
      flag = 'N'
    }
    this.setData({
      'addr.defaultFlag':flag
    })
  },
  changeLocation(){
    let that = this
    wx.chooseLocation({
      success: (res) => {
        console.log(res)
        let addName="";
        if(res.name){
          addName=res.name;
        }else{
          addName=res.address;
        }
        if(addName == ""){
          // that.initLocation()
        }else{
          this.setData({
            'addr.latitude':res.latitude,
            'addr.longitude':res.longitude
          })
          //纬度
          var latitude = res.latitude
          //经度
          var longitude = res.longitude
          that.reverseLocation(latitude,longitude)
        }
      },
      complete(res){
      }
    })
  },
  reverseLocation(latitude,longitude){
    let that = this
    qqMapSdk = new QQMapWX({
      key: config.QQMapKey
    });
    qqMapSdk.reverseGeocoder({
      location:{latitude:latitude,longitude:longitude},
      complete(res){
        if(!res.status && res.status==0){
          that.data.locationFail=false;
          let address_reference=res.result.address_reference;
          let locationAddr=res.result.address;
          if(address_reference){
            let landmark_l2=address_reference.landmark_l2;
            if(landmark_l2){
              locationAddr=landmark_l2.title;
            }
            let landmark_l1=address_reference.landmark_l1;
            if(landmark_l1){
              locationAddr=landmark_l1.title;
            }
          }
          const {ad_info} = res.result
          const {address_component}= res.result
          let provinceCode = ad_info.adcode.substr(0,2)+'0000'
          that.setData({
            'addr.provinceName':address_component.province,
            'addr.cityName':address_component.city,
            'addr.areaName':address_component.district,
            'addr.streetName':address_component.street,
            'addr.adcCode':ad_info.adcode,
            'addr.proviceCode':provinceCode,
            adName: address_component.province+','+address_component.city+','+address_component.district
          })
          if(that.data.cityList.includes(ad_info.city)){
            let cityCode = ad_info.adcode.substr(0,2) + '0000'
            that.setData({
              'addr.cityCode':cityCode,
            })
          }else{
            let cityCode = ad_info.adcode.substr(0,4) + '00'
            that.setData({
              'addr.cityCode':cityCode,
            })
          }
          
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  save(){
    let addr = this.data.addr
    this.setData({subState:true});
  debugger
  if(!addr.recipientName){
    wx.showToast({
      title: '请填写收货人姓名',
      icon: 'none'
    })
    this.setData({subState:false});
    return;
  }
  if(!addr.phone){
    wx.showToast({
      title: '请填写收货人电话',
      icon: 'none'
    })
    this.setData({subState:false});
    return;
  }
  if(!this.data.adName){
    wx.showToast({
      title: '请选择所在地区',
      icon: 'none'
    })
    this.setData({subState:false});
    return;
  }
  if(!addr.addressName){
    wx.showToast({
      title: '请填写详细地址',
      icon: 'none'
    })
    this.setData({subState:false});
    return;
  }


    if(addr.id){
      Api_Mall_Address.edit(addr).then(res=>{
        let pages = getCurrentPages()
        pages[pages.length - 2].loadData()
        wx.navigateBack({
            delta: 0,
        })
      })
    }else{
      Api_Mall_Address.save(addr).then(res=>{
        let pages = getCurrentPages()
        pages[pages.length - 2].loadData()
        wx.navigateBack({
            delta: 0,
        })
      })
    }
  }
})